<?php
include("./config.php");
include("./utils.php");
?>
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>快捷键</title>
  <?php include 'scripts-inc.php' ?>
  
  <script>

  function preview_column_template(){/*
  <section class="clearfix wrap-cate">
    <header class="sub-title">
      <h3>{head}</h3>
      <p class="text-muted">{des}</p>
    </header>
    <table class="table table-striped">{hrefs}</table>
  </section>
  */}
  function preview_item_template(){/*
<tr class="skin{skin}">
  <td>{i}</td>
  <td>{shortcut}</td>
  <td>{name}
  <div class="text-warning"><strong>{context}</strong></div>
  <div class="text-muted">{des}</div>
  </td>
</tr>
  */}

  function commit_data_template(){/*
    共{count}个数据。
*/}




var gAppID=<?php echo getFromMap($_GET, 'appid', 0); ?>;
var gLang='<?php echo getFromMap($_GET, 'lang', 'cn'); ?>';

var gData = {};
var gDataList = [];




$(function(){
  var u = 'query.php?op=one-app&appid='+gAppID;
  $.ajax({url:u,
    dataType:"json",
    async:true,
    success : function(msg, textStatus) { 
      var data = msg[0];
      if(data){
        $("#tid").val(data["appid"]);
        $("#tname").val(data["name"]);
        $("#tdes").val(data["des"]);
        $("#tdes_en").val(data["des_en"]);
        $("#tname_en").val(data["name_en"]);
        $("#tskin").val(data["skin"]);
        $("#tlogo").val(data["logo"]);
        $("#logo-img").attr("src", data['logo']);

        $(document).attr("title",'添加[' + data['name']+']');//修改title值
        var appid = data['appid'];
        $("a#goback-app").attr("href","./show-app.php?appid="+appid);
        gData = data;
        gData.des = getMarkdown(gData.des);

      }
    }
  },"text");

  $("#commit-data").unbind("click").click(function(){
    if(!gAppID){
      var msg = "非法操作，请确认选择提交的是哪个软件的快捷键！";
      alert(msg);
      show_msg(msg);
      return;
    }

    $("#preview-result").trigger("click");
    var data = {
      "userid":gUserID,
      "data":gDataList,
      "appid":gAppID,
      op:'add-shortcuts'
    };
    show_msg("#commit-result", "正在提交中...");
    $("#commit-data").attr({"disabled":"disabled"});
    $.post("update-data.php", data, function(msg){
      show_msg("#commit-result", "提交完成");
      $("#commit-data").hide();
    });

  });

  $("#clear-preview").unbind("click").click(function(){
    $("#data-result").html("");
  });

  $("#preview-result").unbind("click").click(function(){
    var s = $("#data-content").val();
    var lines = s.split("\n");
    var count = 0;
    var hrefs = "";
    gDataList = [];
    for(var i in lines){
      var line = lines[i];
      if (line.trim() == "") continue;
      var items = line.split("\t");
      var data = {};
      var index = 0;
      data.i = count + 1;
      data.shortcut = items[index++];
      data.name = items[index++];
      data.des = gdefault(items[index++]);
      data.context = gdefault(items[index++]);
      data.skin = gdefault(items[index++], 0);
      data.name_en = gdefault(items[index++]);
      data.des_en = gdefault(items[index++]);
      data.context_en = gdefault(items[index++]);


      gDataList.push(data);
      var s = hereTemplate(preview_item_template, data);
      hrefs += s;
      count += 1;
    }

    if (gDataList == []){
      show_msg("没有内容！");
      return;
    }


    var data={"count":count};
    var result = hereTemplate(commit_data_template, data);
    $("#commit-result").html(result);

    data={"head":gData.name,"hrefs":hrefs, 
      "des":gData.des, "appid":gData.appid};
    result = hereTemplate(preview_column_template, data);
    $("#data-result").html(result);
    
    $(".skin1").addClass("list-group-item-danger");
    $(".skin2").addClass("list-group-item-info");
    $(".skin3").addClass("list-group-item-warning");
    $(".skin4").addClass("list-group-item-success");
    $(".skin5").addClass("list-group-item-purple");

  });

});//end of $(function(){
  </script>
</head>
<body>

<div id="wrapper">
<div id="container">

<div id="nav_data_src">
  <?php include 'navbars-inc.php' ?>
</div>


<div class="clearfix"></div>
</div><!-- end of search-container -->

<div class="clearfix"></div>

    <div class="form-group row">
      <label for="tid" class="col-xs-3 col-sm-1 col-md-1 col-lg-1 control-label">ID</label>
      <div class="col-xs-7 col-sm-8 col-md-8 col-lg-8">
        <input type="" class="form-control" id="tid" name="tid" placeholder="" disabled/>
      </div>
      <a href="javascript:window.opener=null;window.open('','_self');window.close();"
       class="lang-close btn btn-danger col-xs-2 col-sm-1 col-md-1 col-lg-1 text-center" 
       id="" >关闭</a>
    </div>

    <div class="form-group row">
      <label for="tname" class="col-xs-3 col-sm-1 col-md-1 col-lg-1 control-label">软件</label>
      <div class="col-xs-7 col-sm-8 col-md-8 col-lg-8">
        <input type="" class="form-control" id="tname" name="tname" placeholder="" disabled/>
      </div>
      <a href="./index.php" id="goback-app" 
        class="btn btn-warning col-xs-2 col-sm-1 col-md-1 col-lg-1 text-center" >返回列表</a>
    </div>

    <div class="form-group row">
      <label for="commit-info" class="col-xs-3 col-sm-1 col-md-1 col-lg-1 control-label">说明</label>
      <div class="col-xs-7 col-sm-8 col-md-8 col-lg-8">
        <div id="commit-info"><p>一行一个数据，每行用TAB分隔，可先用文本编辑器编辑好后复制过来。<strong>请先预览后提交数据</strong></p>
        <p>按如下顺序进行：1-快捷键-(shortcut), 2-快捷键作用-(name), 3-快捷键描述-(des), 4-快捷键上下文-(context), 5-皮肤-(skin), 6-快捷键作用的英文-(name_en), 7-快捷键描述的英文-(des_en), 8-快捷键上下文的英文-(context_en)</p></div>
      </div>
      <button type="submit" class="btn btn-primary col-xs-2 col-sm-1 col-md-1 col-lg-1" id="commit-data">提交数据</button>
    </div>

    <div class="form-group row">
      <label for="data-content" class="col-xs-3 col-sm-1 col-md-1 col-lg-1 control-label">数据</label>
      <div class="col-xs-7 col-sm-8 col-md-8 col-lg-8">
        <textarea class="form-control" id="data-content" rows="12" name="data-content" placeholder=""></textarea>
      </div>
      <button type="submit" class="btn btn-success col-xs-2 col-sm-1 col-md-1 col-lg-1" id="preview-result">生成预览</button>
    </div>
    <div class="form-group row">
      <label for="commit-result" class="col-xs-3 col-sm-1 col-md-1 col-lg-1 control-label">统计</label>
      <div class="col-xs-7 col-sm-8 col-md-8 col-lg-8">
        <div id="commit-result"></div>
      </div>
    </div>

    <div class="form-group row">
      <label for="data-result" class="col-xs-3 col-sm-1 col-md-1 col-lg-1 control-label">预览</label>
      <div class="col-xs-7 col-sm-8 col-md-8 col-lg-8">
        <div id="data-result"></div>
      </div>
      <button type="submit" class="btn btn-default col-xs-2 col-sm-1 col-md-1 col-lg-1" id="clear-preview">清空预览</button>
    </div>


  <div class="clearfix"></div>
  <div class="form-group row">
  </div>

  <div class="form-group row">
    <div class="col-xs-8 col-sm-3 col-md-3 col-lg-3 text-center">
      <div id="content"></div>
    </div>
  </div>


<div class="clearfix"></div>
<div id="hide-result"> </div>
<!-- .................................................... -->
</div><!-- end of container -->
</div><!-- end of wrapper -->
</body>
</html>
